/*
 * 3.1.cpp  poj-1565
 *
 *  Created on: 2012-5-17
 *      Author: jawinton
 */


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

#define SIZE 8

long b2ten(char *x, int b) {
	long ret = 0;
	int len = strlen(x);
	for (int i=0; i < len; i++) {
		if ((x[i]-'0') >= b) return -1;
		ret *= b;
		ret += x[i]-'0';
	}
	return ret;
}

int main (void) {
	int n;
	cin >> n;
	while (n--) {
		char p[SIZE], q[SIZE], r[SIZE];
		cin >> p >> q >> r;
		int i;
		for (i=2; i<=16; i++) {
			long l, m, n;
			if (l == -1 || m == -1 || n== -1) continue;
			l = b2ten(p, i);
			m = b2ten(q, i);
			n = b2ten(r, i);
			if (l*m == n) {
				cout << i << endl;
				break;
			}
		}
		if (i > 16) cout << 0 <<endl;
	}
	return 0;
}
